package com.yanqu.road.server.manger.patrons;

import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSimple;
import com.yanqu.road.logic.bussiness.player.UserPatronsBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PatronsSimpleCacheMgr extends TempMgr {

    public static Map<Long, Map<Integer, UserPatronsSimple>> patronsSimpleMap;

    @Override
    public boolean reloadData() throws Exception {
        patronsSimpleMap = UserPatronsBussiness.getUserPatronsSimpleMap();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static List<UserPatronsSimple> getUserPatronsSimpleList(long userId){
        if(patronsSimpleMap.containsKey(userId)){
            return new ArrayList<>(patronsSimpleMap.get(userId).values());
        }
        return null;
    }

    /**
     * 获取门客数量
     * @param userId
     * @return
     */
    public static int getUserPatronsSimpleCount(long userId){
        if(patronsSimpleMap.containsKey(userId)){
            return patronsSimpleMap.get(userId).size();
        }
        return 0;
    }

    /**
     * 变更门客数据
     * @param userPatrons
     */
    public static void changeUserPatronsSimple(UserPatrons userPatrons){
        if(null == userPatrons){
            return;
        }
        if(!patronsSimpleMap.containsKey(userPatrons.getUserId())){
            synchronized (patronsSimpleMap){
                if(!patronsSimpleMap.containsKey(userPatrons.getUserId())){
                    patronsSimpleMap.put(userPatrons.getUserId(), new ConcurrentHashMap<>());
                }
            }
        }
        Map<Integer, UserPatronsSimple> simpleMap = patronsSimpleMap.get(userPatrons.getUserId());
        if(!simpleMap.containsKey(userPatrons.getPatronsId())){
            synchronized (simpleMap){
                if(!simpleMap.containsKey(userPatrons.getPatronsId())){
                    UserPatronsSimple simpleData = parsePatronsSimple(userPatrons);
                    simpleMap.put(simpleData.getPatronsId(), simpleData);
                }
            }
        }
        UserPatronsSimple simpleData = simpleMap.get(userPatrons.getPatronsId());
        simpleData.setAbility(userPatrons.getAbility());
        simpleData.setTradeWarLoseScoreReduce(userPatrons.getTradeWarLoseScoreReduce());
    }

    /**
     * 门客数据转简单数据
     * @param userPatrons
     * @return
     */
    public static UserPatronsSimple parsePatronsSimple(UserPatrons userPatrons){
        if(null == userPatrons){
            return null;
        }
        UserPatronsSimple data = new UserPatronsSimple();
        data.setUserId(userPatrons.getUserId());
        data.setPatronsId(userPatrons.getPatronsId());
        data.setAbility(userPatrons.getAbility());
        data.setTradeWarLoseScoreReduce(userPatrons.getTradeWarLoseScoreReduce());
        return data;
    }

    /**
     * 转门客列表
     * @param patronsSimpleList
     * @return
     */
    public static List<UserPatrons> parseUserPatronsList(List<UserPatronsSimple> patronsSimpleList){
        List<UserPatrons> dataList = new ArrayList<>();
        for(UserPatronsSimple patronsSimple : patronsSimpleList){
            UserPatrons data = new UserPatrons();
            data.setUserId(patronsSimple.getUserId());
            data.setPatronsId(patronsSimple.getPatronsId());
            data.setAbility(patronsSimple.getAbility());
            data.setTradeWarLoseScoreReduce(patronsSimple.getTradeWarLoseScoreReduce());
            dataList.add(data);
        }
        return dataList;
    }
}
